/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Fabienne Boyer - initial API and implementation
 ******************************************************************************/
package fr.jade.reflex.control.reflex;

import java.util.HashMap;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.InstantiationException;

import fr.jade.fraclite.InitializationContext;
import fr.jade.fraclite.api.Controller;
import fr.jade.reflex.api.control.ReflexController;
import fr.jade.util.log.Logger;
import fr.jade.util.log.config.LogReflex;

/**
 * Provides a basic implementation of the {@link ReflexController}
 * ReflexController interface.
 * 
 * @author Fabienne Boyer
 * @author Sylvain Sicard
 * @contact Fabienne.Boyer@inrialpes.fr
 * 
 */

public class BasicReflexController implements Controller, ReflexController {


    // -------------------------------------------------------------------------
    // Fields and methods added and overriden by the mixin class
    // -------------------------------------------------------------------------

    /**
     * This field contains the reference to the dual component
     */
    Component cmp = null;

    /**
     * This field contains the level of the current component
     */
    String level = ReflexController.NO_LEVEL;

    /**
     * The reflex attributes.
     */
    HashMap<String, String> atts = null;

    /**
     * Initializes the fields of this mixin from the given context, and then
     * calls the overriden method.
     * 
     * @param ic
     *            information about the component to which this controller
     *            object belongs.
     * @throws InstantiationException
     *             if the initialization fails.
     */

    public void initFcController(final InitializationContext ic) throws InstantiationException {

    }

    /**
     * set the reference of the dual component : (reference of the associated
     * component at the execution level if this component is at the meta level,
     * reference of the associated component at the meta level if this component
     * is at the execution level.
     * 
     * @param ref
     *            the ref of the dual component
     * 
     * @see fr.jade.reflex.ReflexController#setCmpRef(org.objectweb.fractal.api.Component)
     */
    public void setCmpRef(Component ref) {

        Logger.println(LogReflex.debug, "BasicReflexController.setCmpRef");

        cmp = ref;
    }

    /**
     * return the reference of the dual component : (reference of the associated
     * component at the execution level if this component is at the meta level,
     * reference of the associated component at the meta level if this component
     * is at the execution level.
     * 
     * @return the ref of the dual component
     * @see fr.jade.reflex.ReflexController#getCmpRef()
     */

    public Component getCmpRef() {
        return cmp;
    }

    /**
     * set the level of this component
     * 
     * @param level
     *            the level of the component
     * 
     */
    public void setLevel(String level) {
        this.level = level;
    }

    /**
     * return the level of this component
     * 
     * @return the level of the component
     */
    public String getLevel() {
        return level;
    }

    /**
     * Set a predefined reflex attribute .
     * 
     * @param name
     *            the name of the attribute to set.
     * @param value
     *            the value of the attribute.
     */
    public void setReflexAttribute(String name, String value) {
        if (atts == null)
            atts = new HashMap<String, String>();
        if (value != null)
            atts.put(name, value);
    }

    /**
     * Get the value of a predefined reflex attribute .
     * 
     * @param name
     *            the name of the attribute to set.
     * @return the value of the attribute (null if it does not exist).
     */
    public String getReflexAttribute(String name) {
        return ((String) atts.get(name));
    }

}
